package com.atguigu.ssyx.acl.controller;

import com.atguigu.ssyx.acl.service.AdminService;
import com.atguigu.ssyx.acl.service.RoleService;
import com.atguigu.ssyx.common.result.Result;
import com.atguigu.ssyx.common.utils.MD5;
import com.atguigu.ssyx.model.acl.Admin;
import com.atguigu.ssyx.vo.acl.AdminQueryVo;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * @author chentengfei
 * @desc 用户管理模块
 * @date 2023-06-11 11:27:30
 */
@Api(tags = "用户接口")
@RestController
@RequestMapping("/admin/acl/user")
// @CrossOrigin
public class AdminControlller {

    /**对应前端页面:ssyx-admin/src/api/acl/user.js**/

    @Autowired
    private AdminService adminService;
    @Autowired
    private RoleService roleService;

    /***
     * 为用户进行分配(参数有用户id 和 多个角色id)
     * //    url: `${api_name}/doAssign`,
     * //    method: 'post',
     * //    params: {
     * //        adminId,
     * //        roleId
     * //    }
     * @author chentengfei
     * @date 2023/6/11 15:06
     * @param adminId
     * @param roleId
     * @return com.atguigu.ssyx.common.result.Result
     */
    @ApiOperation("为用户进行角色分配")
    @PostMapping("doAssign")
    public Result doAssign(@RequestParam Long adminId,@RequestParam Long[] roleId){
        roleService.saveAdminRole(adminId,roleId);
        return Result.ok(null);
    }

    /***
     * 获取所有角色，和根据用户id查询用户分配角色列表
     * //    url: `${api_name}/toAssign/${adminId}`,
     * //    method: 'get'
     * @author chentengfei
     * @date 2023/6/11 15:04
     * @param adminId
     * @return com.atguigu.ssyx.common.result.Result
     */
    @ApiOperation("获取用户角色")
    @GetMapping("toAssign/{adminId}")
    public Result toAssign(@PathVariable Long adminId){
        //返回map集合包含两部分数据：所有角色 和 为用户分配角色列表
        Map<String,Object> map = roleService.getRoleByAdminId(adminId);
        return Result.ok(map);
    }

    /***
     * 用户列表
     * @author chentengfei
     * @date 2023/6/11 11:32
     * @param current
     * @param limit
     * @param adminQueryVo
     * @return com.atguigu.ssyx.common.result.Result
     */
    @ApiOperation("用户列表")
    @GetMapping("{current}/{limit}")
    public Result list(@PathVariable Long current, @PathVariable Long limit, AdminQueryVo adminQueryVo){
        Page<Admin> pageParam = new Page<>(current,limit);
        IPage<Admin> pageModel = adminService.selectPageUser(pageParam,adminQueryVo);
        return Result.ok(pageModel);
    }

    /***
     * id查询用户
     * url: `${api_name}/get/${id}`,
     * method: 'get'
     * @author chentengfei
     * @date 2023/6/11 11:34
     * @param id
     * @return com.atguigu.ssyx.common.result.Result
     */
    @ApiOperation("根据id查询")
    @GetMapping("get/{id}")
    public Result get(@PathVariable Long id){
        Admin admin = adminService.getById(id);
        return Result.ok(admin);
    }

    /***
     * 3 添加用户
     * url: `${api_name}/save`,
     * method: 'post',
     * data: user
     * @author chentengfei
     * @date 2023/6/11 11:41
     * @param admin
     * @return com.atguigu.ssyx.common.result.Result
     */
    @ApiOperation("添加用户")
    @PostMapping("save")
    public Result save(@RequestBody Admin admin){
        //获取输入的密码
        String password = admin.getPassword();
        //对输入密码进行加密 MD5
        String passwordMD5 = MD5.encrypt(password);
        //设置到admin对象里面
        admin.setPassword(passwordMD5);
        //调用方法添加
        adminService.save(admin);
        return Result.ok(null);
    }

    /***
     * 修改用户
     * url: `${api_name}/update`,
     * method: 'put',
     * data: user
     * @author chentengfei
     * @date 2023/6/11 11:42
     * @param admin
     * @return com.atguigu.ssyx.common.result.Result
     */
    @ApiOperation("修改用户")
    @PutMapping("update")
    public Result update(@RequestBody Admin admin) {
        adminService.updateById(admin);
        return Result.ok(null);
    }

    /***
     * id删除
     * url: `${api_name}/remove/${id}`,
     * method: 'delete'
     * @author chentengfei
     * @date 2023/6/11 11:42
     * @param id
     * @return com.atguigu.ssyx.common.result.Result
     */
    @ApiOperation("根据id删除用户")
    @DeleteMapping("remove/{id}")
    public Result remove(@PathVariable Long id) {
        adminService.removeById(id);
        return Result.ok(null);
    }

    /***
     * 批量删除
     * url: `${api_name}/batchRemove`,
     * data: ids
     * [1,2,3]
     * method: 'delete',
     * @author chentengfei
     * @date 2023/6/11 11:42
     * @param idList
     * @return com.atguigu.ssyx.common.result.Result
     */
    @ApiOperation("批量删除")
    @DeleteMapping("batchRemove")
    public Result batchRemove(@RequestBody List<Long> idList) {
        adminService.removeByIds(idList);
        return Result.ok(null);
    }
}
